﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iOak.Domain.Users;
using iOak.Business.Users;
using System.Configuration;

namespace iOak.Web.Administration
{
    public partial class UserList : BasePage
    {
        #region 页面事件
        /// <summary>
        /// 页面初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            pagerControl.PageIndexChange += new Modules.PagerControl.OnPageIndexChangeHandler(pagerControl_PageIndexChange);
            if (!Page.IsPostBack)
            {
                BindUserList();
            }
        }

        protected void pagerControl_PageIndexChange(object serder, EventArgs e)
        {
            BindUserList();
        }

        /// <summary>
        /// gvUser行绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvUser_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                User user = e.Row.DataItem as User;
                Label lblIsActive = e.Row.FindControl("lblIsActive") as Label;
                if (user.Active)
                {
                    lblIsActive.Text = "激活";
                }
                else 
                {
                    lblIsActive.Text = "未激活";
                }
                Label lblIsAdmin = e.Row.FindControl("lblIsAdmin") as Label;
                if (user.IsAdmin)
                {
                    lblIsAdmin.Text = "是";
                }
                else 
                {
                    lblIsAdmin.Text = "否";
                }
            }
        }

        /// <summary>
        /// 检索按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFind_Click(object sender, EventArgs e)
        {
            gvUser.PageIndex = 0;
            BindUserList();
        }

        protected void btnPostBack_Click(object sender, EventArgs e)
        {
            Guid id;
            switch (hidEventName.Value)
            {
                case "delete":
                    id = Guid.Parse(hidEventArguments.Value);
                    DeleteUser(id);
                    break;
            }
        }
        #endregion

        #region 处理函数
        /// <summary>
        /// 绑定User列表
        /// </summary>
        private void BindUserList()
        {
            UserService userService = new UserService();
            //管委会Id
            Guid wndId = Guid.Parse(ConfigurationManager.AppSettings["wndId"]);
            //检索符合条件的user
            List<User> userList = userService.GetListByCompanyId(wndId);
            pagerControl.BindData(gvUser, userList);
        }

        private void DeleteUser(Guid id)
        {
            UserService userService = new UserService();
            userService.Delete(id);
            BindUserList();
        }

        #endregion
    }
}